package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.message.MessageTemplate;
import com.cy.ppj.model.dto.message.OrderScanRecordMessageDTO;
import com.cy.ppj.model.dto.wechat.WecahtTemplateDataDTO;
import com.cy.ppj.model.dto.wechat.WechatTemplateDTO;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.enums.wechat.WechatTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 客户询单（浏览记录）消息通知
 * @Author tanyilin
 * @Date 2019/5/16 14:26
 */
@Slf4j
@Message(type = MessageType.ADD_ORDER_SCAN_RECORD)
@Service
public class OrderScanRecordMessageImpl extends MessageServiceImpl {

    @Value("${ppj.order-scan-record-page}")
    private String myOrderScanRecordPage;

    @Override
    public void execute(String message) {

        //订单编号 orderId
        Long orderId = Long.valueOf(message);
        //查询发送微信消息的订单相关参数
        OrderScanRecordMessageDTO template = messageTemplateMapper.selectOrderScanRecordMessageTemplate(orderId);
        if(template != null && !"dev".equals(Constants.PROFILES_ACTIVE)) {
            //发送微信消息
            sendWechatTemplate(template);
        }
        //发送站内信
        sendMessage(template);
    }

    /**
     * 发送站内信
     *
     * @param template
     */
    private void sendMessage(OrderScanRecordMessageDTO template) {

        MessageTemplate messageTemplate = messageTemplateService.queryMessageTemplateByTypeInCache(MessageType.ADD_ORDER_SCAN_RECORD);
        if (messageTemplate == null) {
            log.info("贷款新客户询单[{}]提醒,没有消息模版。", template.getOrderId());
            return;
        }
        if (!messageTemplate.getOpen()) {
            log.info("贷款新客户询单[{}]提醒,消息模版未开启。", template.getOrderId());
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("createTime", CommonUtils.dateFormat(template.getCreateTime()));
        map.put("customerName", template.getCustomerName());
        map.put("productName", template.getProductName());

        String content = SpringELUtils.getValue(messageTemplate.getContent(), map);

        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setType(MessageType.ADD_ORDER_SCAN_RECORD);
        messageRecord.setContent(content);
        messageRecord.setUserId(template.getUserId());
        messageRecord.setCreateTime(new Date());
        messageRecord.setUrl("");
        messageRecordService.addMessageRecord(messageRecord);
    }

    /**
     * 发送微信消息模板
     * @param template
     * @return void
     */
    private void sendWechatTemplate(OrderScanRecordMessageDTO template) {

        if (template.getOpenId() == null) {
            log.info("贷款新客户询单提醒，由于经纪人[{}]没有绑定微信，无法发送微信消息。", template.getUserId());
            return;
        }

        //封装发送微信消息参数
        WechatTemplateDTO send = new WechatTemplateDTO();
        send.setOpneId(template.getOpenId());
        send.setAccessToken(wechatService.getAccessToken());
        send.setTemplateId(WechatTemplate.ADD_ORDER_SCAN_RECORD.getTemplateId());
        send.setUrl(myOrderScanRecordPage);

        WecahtTemplateDataDTO first = new WecahtTemplateDataDTO();
        first.setValue("有新的客户通过您的二维码扫码询单，请及时查看处理哦。");

        WecahtTemplateDataDTO keyword1 = new WecahtTemplateDataDTO();
        keyword1.setValue(CommonUtils.dateFormat(template.getCreateTime()));

        WecahtTemplateDataDTO keyword2 = new WecahtTemplateDataDTO();
        keyword2.setValue(template.getCustomerName());

        WecahtTemplateDataDTO keyword3 = new WecahtTemplateDataDTO();
        keyword3.setValue("展业二维码扫码询单");

        WecahtTemplateDataDTO keyword4 = new WecahtTemplateDataDTO();
        keyword4.setValue(template.getProductName());

        WecahtTemplateDataDTO remark = new WecahtTemplateDataDTO();
        remark.setValue("您可在匹配家-我的-询单中订单查看客户提交信息，及时进行交单或其他操作。");

        send.getData().put("first", first);
        send.getData().put("keyword1", keyword1);
        send.getData().put("keyword2", keyword2);
        send.getData().put("keyword3", keyword3);
        send.getData().put("keyword4", keyword4);
        send.getData().put("remark", remark);

        //发送微信消息模板
        wechatService.sendTemplateMessage(send);

    }

}
